
Page({
  data: {
    intoView: "",
    dats: ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
    cc: 'A',
    cheightList: [],
    timer: null,
    containerH: 0
  },
  onPageScroll: function(e) {

  },
  cx: function(e) {
    this.setData({
      cc: e.currentTarget.dataset.id,
      intoView: "view" + e.currentTarget.dataset.id
    })
  },
  onLoad() {
    let that = this;
    let query = wx.createSelectorQuery().in(this);
    let cheightList = [];
    let s = 0;
    query.selectAll('.cbox').boundingClientRect((react) => {
      react.forEach((res) => {
        s += res.height;
        cheightList.push(s)
      });
      this.setData({
        cheightList: cheightList
      })
    });
    query.select('.conbox').boundingClientRect((res) => {
      // 计算容器高度
      this.setData({
        containerH: res.height
      })
    }).exec()
  },
  onScroll(e) {
    let timer = this.data.timer;
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      let scrollTop = e.detail.scrollTop;
      let scrollArr = this.data.cheightList;
      if (scrollTop >= scrollArr[scrollArr.length - 1] - this.data.containerH) {
        return
      } else {
        for (let i = 0; i < scrollArr.length; i++) {
          if (scrollTop >= 0 && scrollTop < scrollArr[0]) {
            this.setData({
              cc: "A"
            })
          } else if (scrollTop >= scrollArr[i - 1] && scrollTop < scrollArr[i]) {
            this.setData({
              cc: this.data.dats[i]
            })
          }
        }
      }
    },50)
  }
})